package com.mudongheng.app.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 幕冬
 * @since 2022年03月15日
 */
@Aspect
@Component
public class MuLog {

    @Pointcut("execution(* com.mudongheng.app.service.impl.*.*(..))")
    private void pointcut() {}

    @Around("pointcut()")
    public Object aroundLog(ProceedingJoinPoint point) {
        Object res = null;
        try {
            Object[] args = point.getArgs();
            System.out.println(new Date() + "[" + point.getTarget().getClass().getName() + "]" + " 开始执行");
            res = point.proceed(args);
            System.out.println(new Date() + "[" + point.getTarget().getClass().getName() + "]" + " 执行完成");
        } catch(Throwable e) {
            System.out.println(new Date() + "[" + point.getTarget().getClass().getName() + "]" + " 执行异常");
            e.printStackTrace();
        }
        return res;
    }

}
